
var bsurl = require('../../utils/baseUrl.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: "",
    identity: 0,
    name: "",
    shopName: "",
    phone: "",
    pwd: "",
    pwd1: "",
    tips: "提示",
    hidden: false
  },
  textinput: function (event) {
    var type = event.currentTarget.dataset.type;
    switch (Number(type)) {
      case 1:this.setData({ userName: event.detail.value });break;
      case 2:this.setData({ name: event.detail.value });break;
      case 3:this.setData({ shopName: event.detail.value });break;
      case 4:this.setData({ phone: event.detail.value });break;
      case 5:this.setData({ pwd: event.detail.value });break;
      case 6:this.setData({ pwd1: event.detail.value });break;
    }
  },
  register: function () {
    this.setData({
      hidden: false
    })
    var str = "";
    if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(this.data.pwd)) {
      str = '密码6到20位，包括字母，数字';
    }
    if (!/^[a-zA-Z0-9_-]{4,16}$/.test(this.data.userName)){
      str = '用户名4到16位（字母，数字，下划线，减号）';
    }
    if (!/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/.test(this.data.phone)) {
      str = '手机号格式不对';
    }
    if (this.data.shopName == "" && this.data.identity == "1") {
      str = '店铺名不能为空';
    }
    if (this.data.name == "" && this.data.identity == "0") {
      str = '昵称不能为空';
    }
    if (this.data.pwd != this.data.pwd1) {
      str = '2次输入密码不一致';
    }
    if (str != "") {
      this.setData({
        hidden: true,
        tips: str
      })
      return;
    }
    wx.showToast({
      title: '注册中...',
      icon: "loading",
      duration: 150000
    })
    wx.request({
      url: bsurl + "user/register.do",
      method: "post",
      data: {
        body: {
          username: this.data.userName,
          identity: this.data.identity,
          name1: this.data.name,
          shopname: this.data.shopName,
          phone: this.data.phone,
          userpwd: this.data.pwd,
          money: "",
          prestige: ""
        }
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideToast();
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "请求失败",
          });
          return;
        }
        console.log(res.data);
        if (res.data.success) {
          wx.showToast({
            title: res.data.message,
            duration:3000,
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
          });
        }
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '提示',
          content: "网络请求失败",
        });
      }
    })

  },
  onLoad: function (options) {
    this.setData({ identity: options.identity });
    var str = "";
    if (options.identity == "0")
      str = "客户注册";
    else {
      str = "商家注册";
    }
    wx.setNavigationBarTitle({
      title: str,
    })
  },
  confirm:function(pattern,str,tip){
    if(!patttern.test(str)){
      this.setData({
        hidden:true,
        tips:tip
      })
      return false;
    }
    this.setData({
      hidden: false
    })
    return true;
  }
})